#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;

int n, m;
ll A[N], B[N];
ll extgcd(ll a, ll b, ll& x, ll& y) {
  if (b == 0) {
    x = 1, y = 0;
    return a;
  }
  ll d = extgcd(b, a % b, y, x);
  y -= a / b * x;
  return d;
}
void merge(int i) {
  // X = Ai * x + Bi = Ai+1 * y + Bi+1
  // Ai * x + Ai+1 * y = Bi + 1 - Bi
  ll a = A[i], b = A[i + 1], c = B[i + 1] - B[i];
  ll x, y;
  ll d = extgcd(a, b, x, y);
  if (c % d) {
    cout << 0;
    exit(0);
  }
  c /= d, a /= d, b /= d;
  // x = x * c + k * b
  a = A[i] * b;
  x = ((__int128_t)x * c % a * A[i] % a + B[i]) % a;
  if (x < 0) x += a;
  A[i + 1] = a, B[i + 1] = x;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  rep(i, 1, n) cin >> A[i] >> B[i];
  rep(i, 1, n - 1) merge(i);
  cout << B[n];
  return 0;
}